package com.kokomi.config;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.*;

import java.time.Duration;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Set;


@SpringBootTest
public class TestRedisTemplate {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void testString() {
        ValueOperations ops = redisTemplate.opsForValue();
        ops.set("city", "北京");
        System.out.println(ops.get("city"));

    }

    @Test
    public void bookTest(){
        ValueOperations ops = redisTemplate.opsForValue();
        Book book = new Book(1, "西游记", 28.56d, "人民出版社", LocalDate.now(), "吴承恩");
        ops.set("book:1", book, Duration.ofSeconds(60));
        System.out.println(ops.get("book:1"));
    }

    @Test
    public void testHash(){
        HashOperations pos = redisTemplate.opsForHash();

        // 添加多个元素
        pos.put("car:100","name","凯迪拉克");
        pos.put("car:100","price","100000");
        pos.put("car:100","color","黑色");

        Object name = pos.get("car:100", "name");
        System.out.println(name);
        // 获取所有的字段和值
        Map<Object, Object> entries = pos.entries("car:100");
        System.out.println("所有字段和值"+entries);
        // 所有的键
        Set<String> keys = pos.keys("car:100");
        System.out.println("所有键"+keys);
        // 所有的值
        List<Object> values = pos.values("car:100");
        System.out.println("所有值"+values);


    }
    @Test
    public void testList(){
        ListOperations ops = redisTemplate.opsForList();
        Book book = new Book(1, "西游记", 28.56d, "人民出版社", LocalDate.now(), "吴承恩");
        Book book2 = new Book(2, "三国演义", 28.56d, "人民出版社", LocalDate.now(), "罗贯中");
        Book book3 = new Book(3, "水浒传", 28.56d, "人民出版社", LocalDate.now(), "施耐庵");
        Book book4 = new Book(4, "红楼梦", 28.56d, "人民出版社", LocalDate.now(), "曹雪芹");
        Long rows = ops.leftPushAll("bookList", book, book2, book3, book4);
        System.out.println("添加了" + rows + "个元素");
        System.out.println(ops.range("bookList", 0, -1));

    }
    @Test
    public void testSet(){
        SetOperations ops = redisTemplate.opsForSet();
        ops.add("num", 1,2,3,4,5,6,7,8,9,10);
        ops.add("num2", 2,4,6,8,10);

        Set difference = ops.difference("num", "num2");
        System.out.println("差集"+difference);

        Set union = ops.union("num", "num2");
        System.out.println("并集"+union);

        Set intersect = ops.intersect("num", "num2");
        System.out.println("交集"+intersect);

    }

    @Test
    public void testZSet(){
        ZSetOperations ops = redisTemplate.opsForZSet();
        ops.add("book", "西游记", 1);
        ops.add("book", "三国演义", 2);
        ops.add("book", "水浒传", 3);
        ops.add("book", "红楼梦", 4);

        Set book = ops.range("book", 0, -1);
        System.out.println(book);

        // 获取元素的排名
        Long rank = ops.rank("book", "西游记");
        System.out.println(rank);
    }

}
